import lotteryHeaderTemplate from "./lotteryheader.html";

class LotteryHeaderController {
    constructor($q,$interval, lotteryService, $state,durationUtils, constant, $log) {
        Object.assign(this, {$q,$interval, lotteryService,$state, durationUtils, constant, $log})
        this.elapseTimes = 0;
        this.hours = 0;
        this.minutes = 0;
        this.seconds = 0;
        this.interval = null;
        this.lotteryType = null;
        this.lottery = null;
        this.lotteryName = null;
        this.winningList = null;//中奖信息
    }

    /**
     * 获取开奖列表
     */
    getWinningInfo() {
        let self = this;
        this.lotteryService.getWinningInfo(this.lotteryService.lotteryType)
            .then(_winningInfoSucc)
            .catch(_winningInfoError);

        function _winningInfoSucc(data) {
            let dataList = data.data.data_list;
            dataList = dataList.sort((win1, win2) => {
                return Number.parseInt(win1.issue_id) - Number.parseInt(win2.issue_id);
            }).map((win) => {
                win["wined_ball_array"] =
                    win.award_num.split(" ").map((x) => Number.parseInt(x)).sort();
                //console.log(win["wined_ball_array"]);
                return win;
            });
            self.winningList = dataList;
        }

        function _winningInfoError(data) {
            //弹出警告窗口
            console.log(data);
        }
    }

    /**
     * 获取当期信息
     */
    getCurrentIssue() {
        let self = this;
        self.lotteryService.getCurrentIssue(self.lotteryService.lotteryType)
            .then(_currentIssueSucc)
            .catch(_currentIssueError);

        function _currentIssueSucc(data) {
            if(data.message && data.message.code ==200){
                self.lottery = data;
                self.lotteryService.lotteryIssueInfo =data;
                self.lotteryService.setLotteryIssueInfo(data);
                // let startTime = self.durationUtils.parseToDate(data.time_startsale);
                let startTime = self.durationUtils.parseToDate(data.curr_time);
                let endTime = self.durationUtils.parseToDate(data.time_endsale);
                //let openRewardTime = self.durationUtils.parseToDate(data.time_openaward);
                self.elapseTimes = parseInt(self.durationUtils.calcDuration(startTime, endTime) / 1000);
                console.log("time_endsale: "+data.time_endsale +" ,curr_time: "+data.curr_time +" ,elapseTimes: "+ self.elapseTimes+"秒")
                if(!self.elapseTimes){
                    self.elapseTimes = 0;
                }
                if(self.elapseTimes <=0){
                   return self.getCurrentIssue();
                }
                // console.log("startTime: " + startTime.toString() + " ,endTime: " + endTime.toString()
                //             + " ,openRewardTime:  " + openRewardTime.toString() + " ,elapseTimes: "
                //             + self.elapseTimes);
                self.interval = self.$interval(() => {
                    self.hours = self.durationUtils.prependZero((self.elapseTimes / 3600));
                    if(self.hours<=0){
                        self.hours =0;
                    }
                    self.minutes = self.durationUtils.prependZero(((self.elapseTimes %3600)/ 60));
                    if(self.minutes<=0){
                        self.minutes =0;
                    }
                    self.seconds = self.durationUtils.prependZero((self.elapseTimes % 60));
                    if(self.seconds<=0){
                        self.seconds =0;
                    }
                    self.elapseTimes -= 1;
                    if (self.elapseTimes <= 0) {
                        self.elapseTimes=0;
                        self.$interval.cancel(self.interval);
                        self.interval = undefined;
                        layer.open({
                           icon:1,
                           title:"注意！",
                           content:"当前期次投注已经截止，投注时请注意期次！",
                           btn:["确定"],
                           time: 5000,
                           shadeClose:true,
                           yes:function (index,layerno) {
                               layer.close(index);
                               self.$state.go(self.$state.current, {type:self.lotteryService.lotteryType}, {reload: true});
                           },
                            end:function () {
                                self.$state.go(self.$state.current, {type:self.lotteryService.lotteryType}, {reload: true});
                            }
                       });
                    }
                }, 1000);
            }else{
                return self.$q.reject(data.message);
            }
        }

        function _currentIssueError(data) {
            //弹出警告窗口
            console.log(data);
        }
    }

    $onInit() {
        let self = this;
        if (this.lotteryService.lotteryType) {
            self.lotteryType = self.lotteryService.lotteryType;
            self.lotteryName = self.constant[self.lotteryType];
            self.getCurrentIssue();
            self.getWinningInfo();
        }

    }

    $onChanges(changesObj) {
    }

    $onDestroy() {
        this.$interval.cancel(this.interval);
    }

    $postLink() {

    }
}

LotteryHeaderController.$inject = [
    "$q",
    "$interval",
    "LotteryService",
    "$state",
    "durationUtils",
    "Constant",
    "$log",
];

class LotteryHeader {
    constructor() {
        this.restrict = "E";
        this.template = lotteryHeaderTemplate;
        this.controller = LotteryHeaderController;
        this.controllerAs = "header";
        this.bindToController = true;
    }
}

LotteryHeader.$inject = [];
export default LotteryHeader;